/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package G3.Mod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import java.io.IOException;

/**
 *
 * @author Dymion
 */
public class app2sd extends Activity implements OnClickListener {

    private String app2sdContent, app2sdFile;
    private String app2sdV1 = "mkdir /data/misc\n"
            + "mkdir /data/misc/app2sd\n"
            + "LOG=\"/data/misc/app2sd/app2sd.log\"\n"
            + "sleep 3\n"
            + "test_ext4=`fdisk -l /dev/block/mmcblk0 | grep mmcblk0p2 | head -n 1 | cut -d\" \" -f1`\n"
            + "if [ -z $test_ext4 ]; then\n"
            + "\t\t\t\t\techo \"No partition found\" > $LOG\n" + "\t\t\telse\n"
            + "\t\techo \"$(date) App2SD Partition Found\" > $LOG\n"
            + "\t\tbusybox mount -o rw,remount /dev/stl6 /system\n"
            + "\t\tmkdir /system/sd\n"
            + "\t\tbusybox ln -s /proc/partitions /etc/mtab\n"
            + "\t\tbusybox mount -o ro,remount /dev/stl6 /system\n"
            + "\t\tbusybox mount -t ext4 /dev/block/mmcblk0p2 /system/sd 1>>$LOG 2>>$LOG\n"
            + "\t\tmkdir /system/sd/log\n"
            + "\t\tmkdir /system/sd/app\n"
            + "\t\tbusybox mount -o bind /system/sd/app /data/app 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount -o bind /system/sd/log /data/log 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount >> $LOG\n"
            + "\t\techo \"$(date) App2SD Ended\" >> $LOG\n"
            + "\t\t\tfi\n"
            + "sleep 1\n";
    private String app2sdV2 = "mkdir /data/misc\n"
            + "mkdir /data/misc/app2sd\n"
            + "LOG=\"/data/misc/app2sd/app2sd.log\"\n"
            + "sleep 3\n"
            + "test_ext4=`fdisk -l /dev/block/mmcblk0 | grep mmcblk0p2 | head -n 1 | cut -d\" \" -f1`\n"
            + "if [ -z $test_ext4 ]; then\n"
            + "\t\t\t\techo \"No partition found\" > $LOG\n"
            + "\t\t\telse\n"
            + "\t\techo \"$(date) App2SD Partition Found\" > $LOG\n"
            + "\t\tbusybox mount -o rw,remount /dev/stl6 /system\n"
            + "\t\tmkdir /system/sd\n"
            + "\t\tbusybox ln -s /proc/partitions /etc/mtab\n"
            + "\t\tbusybox mount -o ro,remount /dev/stl6 /system\n"
            + "\t\tbusybox mount -t ext4 /dev/block/mmcblk0p2 /system/sd 1>>$LOG 2>>$LOG\n"
            + "\t\tmkdir /system/sd/log\n"
            + "\t\tmkdir /system/sd/app\n"
            + "\t\tmkdir /system/sd/dalvik-cache\n"
            + "\t\tbusybox mount -o bind /system/sd/app /data/app 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount -o bind /system/sd/log /data/log 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount -o bind /system/sd/dalvik-cache /data/dalvik-cache 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount >> $LOG\n"
            + "\t\techo \"$(date) App2SD Ended\" >> $LOG\n"
            + "\t\t\tfi\n"
            + "sleep 1\n";
    private String app2sdV3 = "mkdir /data/misc\n"
            + "mkdir /data/misc/app2sd\n"
            + "LOG=\"/data/misc/app2sd/app2sd.log\"\n"
            + "sleep 3\n"
            + "test_ext4=`fdisk -l /dev/block/mmcblk0 | grep mmcblk0p2 | head -n 1 | cut -d\" \" -f1`\n"
            + "if [ -z $test_ext4 ]; then\n"
            + "\t\t\t\techo \"No partition found\" > $LOG\n"
            + "\t\t\telse\n"
            + "\t\techo \"$(date) App2SD Partition Found\" > $LOG\n"
            + "\t\tbusybox mount -o rw,remount /dev/stl6 /system\n"
            + "\t\tmkdir /system/sd\n"
            + "\t\tbusybox ln -s /proc/partitions /etc/mtab\n"
            + "\t\tbusybox mount -o ro,remount /dev/stl6 /system\n"
            + "\t\tbusybox mount -t ext4 /dev/block/mmcblk0p2 /system/sd 1>>$LOG 2>>$LOG\n"
            + "\t\tmkdir /system/sd/log\n"
            + "\t\tmkdir /system/sd/app\n"
            + "\t\tmkdir /system/sd/dalvik-cache\n"
            + "\t\tmkdir /system/sd/data\n"
            + "\t\tbusybox mount -o bind /system/sd/app /data/app 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount -o bind /system/sd/log /data/log 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount -o bind /system/sd/dalvik-cache /data/dalvik-cache 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount -o bind /system/sd/data /data/data 1>>$LOG 2>>$LOG\n"
            + "\t\tbusybox mount >> $LOG\n"
            + "\t\techo \"$(date) App2SD Ended\" >> $LOG\n"
            + "\t\t\tfi\n"
            + "sleep 1\n";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.app2sd);

        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Make sure your SD-card is partitioned properly.").setPositiveButton("Ok", dialogClickListener).show();
        builder.setCancelable(false);

        //initialize the spinner and set the right adapter
        Spinner app2sdSpinner = (Spinner) findViewById(R.id.app2sd_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.app2sd_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        app2sdSpinner.setAdapter(adapter);

        //initialize textview and button, they need to be changed
        TextView app2sdTxt = (TextView) findViewById(R.id.app2sdTxt);
        Button app2sd = (Button) findViewById(R.id.app2sd);

        //A check if any of the scripts exists
        readfile readV1 = new readfile("/system/etc/init.d/", "S0_app2sd-V1", false);
        readfile readV2 = new readfile("/system/etc/init.d/", "S0_app2sd-V2", false);
        readfile readV3 = new readfile("/system/etc/init.d/", "S0_app2sd-V3", false);
        if (readV1.fileExists()) {
            app2sdTxt.append("Activated.\n Activated version is 1");
            app2sd.setText("Deactivate App2Sd");
        } else if (readV2.fileExists()) {
            app2sdTxt.append("Activated.\n Activated version is 2");
            app2sd.setText("Deactivate App2Sd");
        } else if (readV3.fileExists()) {
            app2sdTxt.append("Activated.\n Activated version is 3");
            app2sd.setText("Deactivate App2Sd");
        } else {
            app2sdTxt.append("Deactivated.");
            app2sd.setText("Activate App2Sd");
        }

        //set the onclicklistener for the button
        View app2sdBtn = findViewById(R.id.app2sd);
        app2sdBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu, menu);
        return (super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.aboutMenu:
                startActivity(new Intent(this, About.class));
                //finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.app2sd:
                final TextView app2sdTxt = (TextView) findViewById(R.id.app2sdTxt);
                final Button app2sd = (Button) findViewById(R.id.app2sd);

                //make /system/ writable
                try {
                    Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "mount -o remount,rw /dev/block/stl6 /system"});
                } catch (IOException ex) {
                    //It doesn't work..
                }

                //initialize 3 readfiles to check for any of the scripts
                readfile readV1 = new readfile("/system/etc/init.d/", "S0_app2sd-V1", false);
                readfile readV2 = new readfile("/system/etc/init.d/", "S0_app2sd-V2", false);
                readfile readV3 = new readfile("/system/etc/init.d/", "S0_app2sd-V3", false);
                if (readV1.fileExists()) { //if app2sd V1 is located in the init.d folder, we delete it! 
                    try {
                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "rm -r /system/etc/init.d/S0_app2sd-V1"});
                    } catch (IOException ex) {
                        //It doesn't work..
                    }

                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system", "reboot now"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    app2sdTxt.setText("App2Sd Deactivated.");
                                    app2sd.setText("Activate App2Sd");
                                    break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("App2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                    builder.setCancelable(false);
                } else if (readV2.fileExists()) { //if app2sd V2 is located in the init.d folder, we delete it! 
                    try {
                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "rm -r /system/etc/init.d/S0_app2sd-V2"});
                    } catch (IOException ex) {
                        //It doesn't work..
                    }

                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system", "reboot now"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    app2sdTxt.setText("App2Sd Deactivated.");
                                    app2sd.setText("Activate App2Sd");
                                    break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("App2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                    builder.setCancelable(false);
                } else if (readV3.fileExists()) { //if app2sd V3 is located in the init.d folder, we delete it! 
                    try {
                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "rm -r /system/etc/init.d/S0_app2sd-V3"});
                    } catch (IOException ex) {
                        //It doesn't work..
                    }

                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system", "reboot now"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    app2sdTxt.setText("App2Sd Deactivated.");
                                    app2sd.setText("Activate App2Sd");
                                    break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("App2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                    builder.setCancelable(false);
                } else { // if there aren't any old app2sd scripts, we create one!
                    //todo: change path and add extra check to see what version was selected in the spinner, if not selected anything -> prompt message
                    Spinner app2sdSpinner = (Spinner) findViewById(R.id.app2sd_spinner);
                    app2sdSpinner.getSelectedItem().toString();
                    if ("App2SD V1".equals(app2sdSpinner.getSelectedItem().toString())) {
                        app2sdContent = app2sdV1;
                        app2sdFile = "S0_app2sd-V1";
                    } else if ("App2SD V2".equals(app2sdSpinner.getSelectedItem().toString())) {
                        app2sdContent = app2sdV2;
                        app2sdFile = "S0_app2sd-V2";
                    } else if ("App2SD V3".equals(app2sdSpinner.getSelectedItem().toString())) {
                        app2sdContent = app2sdV3;
                        app2sdFile = "S0_app2sd-V3";
                    } else {
                        app2sdFile = "Non existent.fail";
                        break;
                    }

                    writefile write = new writefile(app2sdContent, "/system/etc/init.d/", app2sdFile, false);

                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "mount -o remount,ro /dev/block/stl6 /system", "reboot recovery"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot recovery"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    app2sdTxt.setText("App2Sd Activated.");
                                    app2sd.setText("Deactivate App2Sd");
                                    break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("App2Sd activated. \n You have to reboot in recovery and do a wipe data/factory reset. Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                    builder.setCancelable(false);
                }
                break;
        }
    }
}
